package cn.afterturn.easypoi.word.parse.Algorithm;

import cn.afterturn.easypoi.word.bookmarkutil.BooknameUtil;
import cn.afterturn.easypoi.word.entity.bookmark.BookMarkEntityVO;
import cn.afterturn.easypoi.word.entity.bookmark.ImageValueEntityVO;
import cn.afterturn.easypoi.word.entity.bookmark.ParagraphEntityVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import static cn.afterturn.easypoi.util.PoiElUtil.END_STR;
import static cn.afterturn.easypoi.util.PoiElUtil.START_STR;

public abstract class easypoiParsealgo extends ParseParagraphAlgo {
    private static final Logger LOGGER = LoggerFactory.getLogger(easypoiParsealgo.class);

    protected abstract void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, String currentText,
                              List<Integer> runIndex, Map<String, Object> map,String color) throws Exception ;


    @Override
    /**
     * 解析这个段落
     *
     * @param paragraph
     * @param map
     * @author Steven
     * 2020-02-18
     */
        public void parseThisParagraph(XWPFParagraph paragraph, Map<String, Object> map, ParagraphEntityVO paragraphEntityVO, String color) throws Exception{
            System.out.println("进入Paragraph进行解析，该Paragraph的文字内容是 :"+paragraph.getText()+"\r\n");
            XWPFRun run;
            // 拿到的第一个run,用来set值,可以保存格式
            XWPFRun currentRun = null;
            // 存放当前的text
            String currentText = "";
            String text;
            // 判断是不是已经遇到{{
            Boolean isfinde = false;
            // 存储遇到的run,把他们置空
            List<Integer> runIndex = new ArrayList<Integer>();
            int bookmarkno=0;//在一个paragraphy下，可能会有多个bookmark
            List<String> localParagrapbookmarkList=new ArrayList<>();
            for (int i = 0; i < paragraph.getRuns().size(); i++) {
                run = paragraph.getRuns().get(i);
                text = run.getText(0);
                LOGGER.info("当前paragraph划分的第"+i+1+"/"+paragraph.getRuns().size()+"值是:"+text+"\r\n");
                System.out.println("当前paragraph划分的第"+i+"/"+paragraph.getRuns().size()+"值是:"+text+"\r\n");
                if (StringUtils.isEmpty(text)) {
                    continue;
                } // 如果为空或者""这种这继续循环跳过
                if (isfinde) {
                    currentText += text;
                    if (currentText.indexOf(START_STR) == -1) {
                        isfinde = false;
                        runIndex.clear();
                    } else {
                        runIndex.add(i);
                    }
                    if (currentText.indexOf(END_STR) != -1) {
                        //steven add
                        setBookmark(paragraphEntityVO,currentText, bookmarkno);//初步认为在获取bookmark的时候才有效。一般都没有作用。后面要放入到changevaule中进行重构
                        bookmarkno++;
                        localParagrapbookmarkList.add(currentText);
                        changeValues(paragraph, currentRun, currentText, runIndex, map, color);
                        currentText = "";
                        isfinde = false;
                    }
                    // 判断是不是开始
                } else if (text.indexOf(START_STR) >= 0) {
                    currentText = text;
                    isfinde = true;
                    currentRun = run;
                } else {
                    currentText = "";
                }
                if (currentText.indexOf(END_STR) != -1) {
                    //steven add
                    System.out.println("走入到一个run中有全部标签的地方:\r\n");
                    System.out.println("----当前的para下的已经出现的bookmark是:"+currentText+"\r\n");
                    setBookmark(paragraphEntityVO,currentText,bookmarkno);
                    bookmarkno++;
                    if(currentText.contains("已取得工程所在地质量监督部门核发的建筑工程质量等级证")){
                        System.out.println("打印:"+currentText);
                    }
                    localParagrapbookmarkList.add(currentText);
                    //finished
                    changeValues(paragraph, currentRun, currentText, runIndex, map, color);

                    isfinde = false;
                }
            }

            System.out.println("注意：*****************当前Paragraph解析结束，所获取的bookmark总数量为:"+bookmarkno+"\r\n");
            localParagrapbookmarkList.forEach(x->{
                System.out.println("当前的Paragraph所获取到的bookmark:"+x.toString()+"\r\n");
            });

        }
    private void setBookmark(ParagraphEntityVO paragraphEntityVO, String currentText, int bookmarkno) {
        BookMarkEntityVO bookMarkEntityVO =new BookMarkEntityVO();
        bookMarkEntityVO.setId(UUID.randomUUID().toString());
        bookMarkEntityVO.setBookmarkname(currentText);
        bookMarkEntityVO.setBookmarkNoOfPara(bookmarkno);
        bookMarkEntityVO.setBookMarkFromHFPC(paragraphEntityVO.getBookMarkFromHFPC());
        bookMarkEntityVO.setBookmarkType(BooknameUtil.BookMarkType(currentText));//是否是计算字段

        if (currentText.contains("p:")) {
            ImageValueEntityVO imageEntity = new ImageValueEntityVO();
            imageEntity.setId(UUID.randomUUID().toString());
            bookMarkEntityVO.setImageValueEntityVO(imageEntity);
        }
        if(paragraphEntityVO.getBookMarkEntityVOS()==null){
            Set<BookMarkEntityVO> bookMarkEntities=new HashSet<>();
            paragraphEntityVO.setBookMarkEntityVOS(bookMarkEntities);
        }
        paragraphEntityVO.getBookMarkEntityVOS().add(bookMarkEntityVO);
    }

}
